package com.zy.project.system.user.manager.impl;

import com.zy.core.data.jpa.support.manager.impl.BaseManagerImpl;
import com.zy.core.data.jpa.support.repository.BaseRepository;
import com.zy.project.system.user.entity.SystemUser;
import com.zy.project.system.user.manager.SystemUserManager;
import com.zy.project.system.user.repository.SystemUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Package: core.manager.impl
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-9-2 15:24
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
@Service
//默认全局关闭事物,具体需要事物的方法再关掉
@Transactional(readOnly=true)
//缓存组别名称,多个格式{"user1","user2"},表示将缓存到多个组中
//@Cacheable中的value就可以不用写了.
//@CacheConfig(cacheNames = "users")
public class SystemUserManagerImpl extends BaseManagerImpl<SystemUser,String> implements SystemUserManager {

    @Autowired
    private SystemUserRepository systemUserRepository;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public BaseRepository<SystemUser, String> getDao() {
        return this.systemUserRepository;
    }

    //@Cacheable(key = "#username")
    @Override
    public SystemUser findByUsername(String username) {
        return systemUserRepository.findByUsername(username);
    }

    @Override
    @CachePut(key = "#user.id")
    public SystemUser addUser(SystemUser user) {
        return systemUserRepository.save(user);
    }

    @Override
    //@CacheEvict(key = "#user.id")
    public void deleteUser(SystemUser user) {
        systemUserRepository.delete(user);
    }

    @Override
    //@Cacheable(key = "#id")
    public SystemUser findUserById(String id) {
        return systemUserRepository.findOne(id);
    }

    @Override
    public String testGetRedisTemp(String key){
        String s = (String)redisTemplate.opsForValue().get(key);
        return s;
    }

    @Override
    public void testPutRedisTemp(String key, String value){
        redisTemplate.opsForValue().set(key,value);
    }

}
